#include "DatabaseDLL.h"
#include <fstream>
#include <iostream>
#include <algorithm>

namespace Database
{

	void DatabaseHandler::add(std::string fname, std::string lname, std::string phone, std::vector<Entry>* list)
	{
		Entry newEntry;

		newEntry.fname = fname;
		newEntry.lname = lname;
		newEntry.phone = phone;

		list->push_back(newEntry);
	}

	int DatabaseHandler::remove(std::string label, std::vector<Entry>* list, int type)
	{
		// remove by first and last name, must be exact matches divided by space
		if(type == 0)
		{
			std::string name[2];
			name[0] = label.substr(0,label.find_first_of(' '));
			name[1] = label.substr(label.find_first_of(' '));

			for(std::vector<Entry>::iterator it = list->begin(); it!=list->end();it++)
			{
				if(name[0].compare(it->fname) == 0)
				{
					if(name[1].compare(it->lname) == 0)
					{
						list->erase(it);
						return 0;
					}
				}
			}
		}
		// else remove by phone number, must be exact match
		else
		{
			for(std::vector<Entry>::iterator it = list->begin(); it!=list->end();it++)
			{
				if(it->phone.compare(label) == 0)
				{
					list->erase(it);
					return 0;
				}
			}
		}

		return 1;
	}

	std::vector<Entry> DatabaseHandler::find(std::string fstring, std::vector<Entry>* list)
	{
		std::vector<Entry> vEntry;

		for(std::vector<Entry>::iterator it = list->begin(); it!=list->end();it++)
		{
			if(it->fname.find_first_of(fstring) != -1)
			{
				vEntry.push_back(*it);
				continue;
			}
			else if(it->fname.find_first_of(fstring) != -1)
			{
				vEntry.push_back(*it);
				continue;
			}
			else if(it->phone.find_first_of(fstring) != -1)
			{
				vEntry.push_back(*it);
			}
		}

		return vEntry;
	}

	void DatabaseHandler::sort(std::vector<Entry>* list, int order)
	{
		if(order == 1)
		{
			std::sort(list->begin(),list->end(),compareFname);
		}
		else
		{
			std::sort(list->begin(),list->end(),compareLname);
		}
	}

	void DatabaseHandler::save(std::vector<Entry>* list)
	{
		std::ofstream file("phonybooky.pb", std::ios_base::trunc);

		if(file.is_open())
		{
			for(std::vector<Entry>::iterator it = list->begin(); it!=list->end();it++)
			{
				file << it->fname << ";" << it->lname << ";" << it->phone << std::endl;
			}

			file.close();
		}
		else
		{
			std::cerr << "File could not be opened for writing." << std::endl;
		}
	}

	std::vector<Entry>* DatabaseHandler::load(int order)
	{
		std::vector<Entry>* vEntry = new std::vector<Entry>();
		Entry newEntry;
		std::ifstream file("phonybooky.pb");
		std::string line;
		int cutAt[2];

		if(file.is_open())
		{
			while(file.good())
			{
				std::getline(file, line);
				if(line != "")
				{
					cutAt[0] = line.find_first_of(';');
					newEntry.fname = line.substr(0,cutAt[0]);
					cutAt[1] = line.find_last_of(';');
					newEntry.lname = line.substr(cutAt[0]+1,cutAt[1]-cutAt[0]-1);
					newEntry.phone = line.substr(cutAt[1]+1,line.length()-cutAt[1]-1);

					vEntry->push_back(newEntry);
				}
			}
			file.close();
			DatabaseHandler::sort(vEntry,order);
		}
		else
		{
			std::cerr << "File read failed." << std::endl;
		}

		return vEntry;
	}

	bool DatabaseHandler::compareFname(Entry i, Entry j)
	{
		return (i.fname.compare(j.fname) < 0);
	}

	bool DatabaseHandler::compareLname(Entry i, Entry j)
	{
		return (i.lname.compare(j.lname) < 0);
	}
}